package cn.hk.vaccination.service.impl;

import cn.hk.common.entity.PlanTypeConstant;
import cn.hk.user.domain.UserInfo;
import cn.hk.user.service.UserInfoService;
import cn.hk.vaccination.domain.PlanChrInoculation;
import cn.hk.vaccination.domain.VaccinesPlan;
import cn.hk.vaccination.domain.vo.VaccinationVo;
import cn.hk.vaccination.mapper.VaccinesPlanMapper;
import cn.hk.vaccination.service.DeptVaccinesStockService;
import cn.hk.vaccination.service.PlanChrInoculationService;
import cn.hk.vaccination.service.VaccinesPlanService;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * <p>
 * 儿童接种计划关联别 服务实现类
 * </p>
 *
 * @author xu
 * @since 2023-03-10
 */
@Service
public class VaccinesPlanServiceImpl extends ServiceImpl<VaccinesPlanMapper, VaccinesPlan> implements VaccinesPlanService {

    @Autowired
    private PlanChrInoculationService planChrInoculationService;
    @Autowired
    private DeptVaccinesStockService deptVaccinesStockService;

    @Autowired
    private VaccinesPlanMapper vaccinesPlanMapper;

    @Autowired
    private  UserInfoService userInfoService;


    @Override
    public List<VaccinationVo> getVaccinationVoListByChr(  Long deptId  , Double monthValue) {
        List<VaccinationVo> vaccinationVoList = new ArrayList<>();
        try {
            vaccinationVoList = vaccinesPlanMapper.selectListByDeptId(deptId,monthValue);
            for (VaccinationVo vaccinationVo : vaccinationVoList) {
                vaccinationVo.setPlanType(PlanTypeConstant.CHILDVACCINATE);
                if (vaccinationVo.getAdequate()==null){
                    vaccinationVo.setAdequate(2);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }

        return vaccinationVoList;
    }

    @Override
    public   List<VaccinationVo> getVaccineList2Chr(Long userId) {
        List<VaccinationVo> resultList= CollUtil.newArrayList();
        UserInfo user = userInfoService.getOneById(userId);
        List<VaccinationVo> vaccinationVos = vaccinesPlanMapper.selectListByDeptId2Chr(user.getDeptId());
        for (VaccinationVo vaccinationVo : vaccinationVos) {
            if (vaccinationVo.getAdequate()==null){
                vaccinationVo.setAdequate(2);
            }
        }

        //根据planAgeMonth排序
        List<PlanChrInoculation> planChrInoculationList =  planChrInoculationService.getList();
        List<PlanChrInoculation> planChrInoculationsSorted = CollUtil.sortByProperty(planChrInoculationList, "planAgeMonth");
        //获取婴儿月份
        Long months = DateUtil.betweenMonth(user.getBirthday(), new Date(), false);
        //排序好的planChrInoculationsSorted遍历,将后面三个返回
        for (int i = 0; i < planChrInoculationsSorted.size()-1; i++) {
            if(planChrInoculationsSorted.get(i).getPlanAgeMonth()>months){
                Integer x = i;
                //遍历vaccinationVos集合
                vaccinationVos.forEach(vaccinationVo -> {
                    //将第一个vaccinationVo放到resultList的集合中
                    if (planChrInoculationsSorted.get(x).getPlanName().equals(vaccinationVo.getPname())){
                        vaccinationVo.setPlanAgeMonth(planChrInoculationsSorted.get(x).getPlanAgeMonth());
                        resultList.add(vaccinationVo);

                    }
                    //将第二个vaccinationVo放到resultList的集合中
                    if (planChrInoculationsSorted.get(x+1).getPlanName().equals(vaccinationVo.getPname())){
                        vaccinationVo.setPlanAgeMonth(planChrInoculationsSorted.get(x+1).getPlanAgeMonth());
                        resultList.add(vaccinationVo);
                    }
//                    //将第三个vaccinationVo放到resultList的集合中
//                    if (planChrInoculationsSorted.get(x+2).getPlanName().equals(vaccinationVo.getPname())){
//                        vaccinationVo.setPlanAgeMonth(planChrInoculationsSorted.get(x+2).getPlanAgeMonth());
//                        resultList.add(vaccinationVo);
//                    }

                });
                break;
            }
        }
        List<VaccinationVo> resultListSorted = CollUtil.sortByProperty(resultList, "planAgeMonth");
        return resultListSorted;
    }


}
